package ATM.dao.impl;

import ATM.dao.BaseDAO;
import ATM.dao.UserDAO;
import ATM.pojo.UserDO;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
//        String sql = "select * from t_user where u_id='"+id+"'and u_pass_word ='"+pwd+"'";
        String sql = "select * from t_user where u_id=? and u_pass_word = ?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getString(1));
                user.setPassWord(rs.getString(2));
                user.setCount(rs.getDouble(3));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(statement, con, rs);
        }
        return user;
    }

    @Override
    public void update(UserDO user) {
        // 1. 创建连接
//        Connection con = getConnection();
//        PreparedStatement statement = null;
        this.getConnection();
        String sql = "update t_user set u_count='" + user.getCount() + "',u_pass_word=" + user.getPassWord() + " where u_id=?";

        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getId());
            //3.执行SQL语句
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(statement, con);
        }
    }


    @Override
    public UserDO getById(String id) {
        this.con = getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
//        String sql = "select * from t_user where u_id='"+id+"'";
        String sql = "select * from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getString(1));
                user.setPassWord(rs.getString(2));
                user.setCount(rs.getDouble(3));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(statement, con, rs);

        }
        return user;

    }

    public void things(UserDO loginUser,UserDO other){
        this.getConnection();
        this.setAutoCommit(false);
        this.update(loginUser);
        this.update(other);
        this.commit();

    }
}
